package com.mew.common.util;

import com.zebra.common.ByteBufUtils;
import io.netty.buffer.ByteBuf;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class PbWriteUtils {

    public static void attr2byteByteBufUtils(Object ob,ByteBuf b) {
        try {
            Class c = ob.getClass();
            Field[] fields = c.getDeclaredFields();
            for (Field field : fields) {
                Class<?> type = field.getType();
                String typeName = field.getType().getName();
                int typeIdex = -1;

                boolean arr = false;
                if (typeName.startsWith("[")){
                    //数组
                    typeName = typeName.replace(";","");
                    arr = true;
                }

                if ((typeIdex = typeName.lastIndexOf("."))>-1){
                    typeName = typeName.substring(typeIdex+1);
                }
                if ("Integer".equals(typeName)){
                    typeName = "int";
                }

                String upName = StringUtils.captureName(field.getName());
                String upTypeName = StringUtils.captureName(typeName);
                Method getmethod = c.getMethod("get" + upName);
                //只处理基本数据类型
                if (StringUtils.basicDataType.contains(upTypeName)){
                    boolean finalArr = arr;
                    Method writemethod = (Method) Arrays.stream(ByteBufUtils.class.getMethods()).filter(m -> ("write" + upTypeName + (finalArr ?"Array":"")).equals(m.getName())).toArray()[0];

                    writemethod.invoke(null,b,getmethod.invoke(ob));
                }else {
                    return;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void attr2byte(Object ob,ByteBuf b) {
        try {
            Class c = ob.getClass();
            Field[] fields = c.getDeclaredFields();
            for (Field field : fields) {
                Class<?> type = field.getType();
                String typeName = field.getType().getName();
                int typeIdex = -1;
                if ((typeIdex = typeName.lastIndexOf("."))>-1){
                    typeName = typeName.substring(typeIdex+1);
                }
                if ("Integer".equals(typeName)){
                    typeName = "int";
                }
                String upName = StringUtils.captureName(field.getName());
                String upTypeName = StringUtils.captureName(typeName);
                Method getmethod = c.getMethod("get" + upName);
                //只处理基本数据类型
                if (StringUtils.basicDataType.contains(upTypeName)){
                    Method writemethod = c.getMethod("write" + upTypeName, ByteBuf.class, type);
                    writemethod.invoke(ob,b,getmethod.invoke(ob));
                }else {
                    return;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void attr2byte2(Object ob,ByteBuf b) {
        try {
            Class c = ob.getClass();
            Field[] fields = c.getDeclaredFields();
            for (Field field : fields) {
                Class<?> type = field.getType();
                String typeName = field.getType().getName();
                int typeIdex = -1;

                boolean arr = false;
                if (typeName.startsWith("[")){
                    //数组
                    typeName = typeName.replace(";","");
                    arr = true;
                }

                if ((typeIdex = typeName.lastIndexOf("."))>-1){
                    typeName = typeName.substring(typeIdex+1);
                }
                if ("Integer".equals(typeName)){
                    typeName = "int";
                }
                String upName = StringUtils.captureName(field.getName());
                String upTypeName = StringUtils.captureName(typeName);
                Method getmethod = c.getMethod("get" + upName);
                //只处理基本数据类型
                if (StringUtils.basicDataType.contains(upTypeName)){
                    boolean finalArr = arr;
                    Method writemethod = c.getMethod("write" + upTypeName+(finalArr ?"Array":""), ByteBuf.class, type);
                    writemethod.invoke(ob,b,getmethod.invoke(ob));
                }else {
                    return;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public static void byte2attr(Object ob,ByteBuf b) {
        try {
            Class c = ob.getClass();
            Field[] fields = c.getDeclaredFields();
            for (Field field : fields) {
                Class<?> type = field.getType();
                String typeName = field.getType().getName();
                int typeIdex = -1;
                if ((typeIdex = typeName.lastIndexOf("."))>-1){
                    typeName = typeName.substring(typeIdex+1);
                }
                if ("Integer".equals(typeName)){
                    typeName = "int";
                }
                String upName = StringUtils.captureName(field.getName());
                String upTypeName = StringUtils.captureName(typeName);
                Method setmethod = c.getMethod("set" + upName);
                //只处理基本数据类型
                if (StringUtils.basicDataType.contains(upTypeName)){

                    Method readmethod = c.getMethod("read" + upTypeName, ByteBuf.class, type);
                    setmethod.invoke(ob,readmethod.invoke(ob,b));
                }else {
                    return;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
